package com.mtnz.njk.service;

import com.mtnz.njk.Mapper.StartImgMapper;
import com.mtnz.njk.Util.DateUtil;
import com.mtnz.njk.Util.DeleteFileUtil;
import com.mtnz.njk.Util.UuidUtil;
import com.mtnz.njk.entity.StartImgEntity;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.util.List;

/**
 * @Description: 首页图片展示
 * @Author: FBL
 * @CreateDate: 2019\7\6 0006 11:32
 * @Version: 1.0
 */
@Service
public class StartImgService {

    @Autowired
    StartImgMapper startImgMapper;

    /**
     * 取出当日应该展示的图片
     *
     * @param startImg
     * @return
     */
    public List<StartImgEntity> showStartImg(StartImgEntity startImg) throws Exception {
        // 当日日期 YYYYMMDD
        String day = DateUtil.getDays();
        Example example = new Example(StartImgEntity.class);
        example.createCriteria().andLike("startImgName", "%" + day + "%");
        if (startImg.getStartImgId() != null) {
            example.createCriteria().andEqualTo("startImgId", startImg.getStartImgId());
        }
        if (startImg.getStartImgName() != null) {
            example.createCriteria().andEqualTo("startImgName", startImg.getStartImgName());
        }
        List<StartImgEntity> startImgEntities = startImgMapper.selectByExample(example);

        return startImgEntities;
    }

    /**
     * 查询所有启动图片
     *
     * @return
     */
    public List<StartImgEntity> queryStartImg() {
        List<StartImgEntity> startImgEntities = startImgMapper.selectAll();
        return startImgEntities;
    }

    /**
     * 保存启动图
     *
     * @param file
     * @return
     */
    public ResponseResult saveStartImg(MultipartFile file) throws Exception {
        // 获取到上传文件的名字
        String fileName = file.getOriginalFilename();
        Example example = new Example(StartImgEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("startImgName",fileName);
        List<StartImgEntity> startImgEntitys = startImgMapper.selectByExample(example);
        if (startImgEntitys != null && startImgEntitys.size() > 0) {
            return new ResponseResult(ResponseMessage.FAIL, "名称不能重复");
        }

        StartImgEntity startImgEntity = new StartImgEntity();
        startImgEntity.setStartImgId(UuidUtil.get32UUID());
        startImgEntity.setCreateTime(DateUtil.getTime());
        startImgEntity.setStartImgName(fileName);
        // 上传图片路径
        File dest = new File(new File(DeleteFileUtil.imgpath).getAbsolutePath() + "/" + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest); // 保存文件
        startImgEntity.setStartImgUrl(DeleteFileUtil.path + "/" + fileName);
        // 保存到数据库中
        startImgMapper.insert(startImgEntity);
        return new ResponseResult(ResponseMessage.OK, DeleteFileUtil.path+"/"+fileName);
    }

    /**
     * 删除启动图
     * @param startImgId
     * @return
     */
    @Transactional
    public ResponseResult delStartImg(String startImgId) throws Exception {
        // 服务器存储图片位置前缀
        String path =  DeleteFileUtil.imgpath+"/";
        StartImgEntity startImgEntity = startImgMapper.selectByPrimaryKey(startImgId);
        if(startImgEntity == null){
            return new ResponseResult(ResponseMessage.DATA_NOT_FOUND);
        }
        path += startImgEntity.getStartImgName();
        // 删除该地址的图片
        DeleteFileUtil.deleteFile(path);
        // 删除数据库记录
        int i = startImgMapper.deleteByPrimaryKey(startImgId);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

}
